'''
Created on 2010-12-21

@author: jstasiaczek
'''
import pygame
class catConfig(object):
    
    keys_to_scan = {}
    pressed_keys = ()
    key_map = {}
    old_action = 'stand'
    inherit_direction = ()
    current_direction = ''

    def __init__(self):
        
        # set up key codes for actions keys
        self.keys_to_scan = {
                             'up': (pygame.K_UP, pygame.K_w),
                             'down': (pygame.K_DOWN, pygame.K_s),
                             'left': (pygame.K_LEFT, pygame.K_a),
                             'right': (pygame.K_RIGHT, pygame.K_d),
                             }
        
        # map of action and key for action. ~ at end mean not pressed
        self.key_map = {
                        'jump' : ('up', 'left~', 'right~'),
                        'jump_move' : ('up', 'right', 'left~'),
                        'jump_move-_back' : ('up', 'left', 'right~'),
                        'move': ('right', 'up~', 'left~'),
                        'move-_back': ('left', 'up~', 'right~'),
                        'hide' : ('down', 'up~', 'left~', 'right~'),
                        'stand' : ('down~', 'up~', 'left~', 'right~'),
                        }
        
        # actions that inheirt direction from main actions
        self.inherit_direction = ('jump', 'hide', 'stand')
        
        
    def __getKey(self, keys):
        pressed = False
        for key in keys:
            if self.pressed_keys[key] == 1:
                pressed = True
        return pressed
    
    def __getKeys(self, pressed_keys):
        keys = {}
        self.pressed_keys = pressed_keys
        for key, val in self.keys_to_scan.items():
            keys[key] = self.__getKey(val)
            keys[key+"~"] = not self.__getKey(val)
        
        return keys
    
    def __getInheritDirection(self, action, direction):
        if action in self.inherit_direction:
            return self.current_direction
        else:
            self.current_direction = direction
            return direction
            
    
    def __parseAction(self, action):
        ''' Return action dict from string action'''
        direction = ''
        if action.find('-') != -1:
            temp = action.split('-')
            direction = temp[1]
            action = temp[0]
        direction = self.__getInheritDirection(action, direction)
        action = action.split('_')
        return {'action': action, 'direction': direction}
    
    def setActionBasedOnKey(self, pressed_keys):
        keys = self.__getKeys(pressed_keys)
        action = self.old_action
        for key,val in self.key_map.items():
            action_ok = True
            for condition in val:
                if not keys[condition]:
                    action_ok = False 
            if action_ok:
                action = key
                break
        
        return self.__parseAction(action)
        
        
        
        
        
        
        
        
        